﻿//======================================================================
//
//        Copyright (C) 苏州逸仙软件    
//        All rights reserved
//
//        filename :knowledgelist_E.aspx
//        description :知识点信息操作
//
//        created by Jason at  创建时间
//
//======================================================================
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace huaguwang.hgmanage.knowledge
{
    public partial class knowledgelist_E : System.Web.UI.Page
    {
        D_T_Knowledge_Exp _DAL = new D_T_Knowledge_Exp();
        M_T_Knowledge _model = new M_T_Knowledge();
        D_T_Category_Exp _catedal = new D_T_Category_Exp();
        private void SetButton(int i, Button but)
        {
            switch (i)
            {
                case 1:
                    but.CausesValidation = false;
                    break;
                default:
                    break;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
            {
                this.WebUserToolBar1.MainInit(
                      UserConst.Exp_GetUser,
                      UserConst.Exp_GetButtonList,
                      SetButton,
                     UserConst.Exp_ToolBarErr
                  );
                if (!IsPostBack)
                {
                    //绑定类别
                    BindCateDrop();
                    if (Request.Params["ID"] != null)
                    {
                        var ID = Request.Params["ID"].Exp_IntTryParse();
                        ViewState["id"] = ID;  
                        //绑定修改信息
                        BindDate(ID);
                    }                    
                }
            });
        }
        /// <summary>
        /// 绑定下拉列表框
        /// </summary>
        private void BindCateDrop()
        {
            AdminPublic.Try(() =>
            {
                this.HGdropCate.Exp_BindClassTree();
            });
        }

        /// <summary>
        /// 修改数据初始化
        /// </summary>
        /// <param name="ID">要修改数据的ID]</param>
        private void BindDate(int ID)
        {
            AdminPublic.Try(() =>
            {
                M_T_Knowledge _model = new M_T_Knowledge();
                _model = _DAL.GetModel(ID);
                txtTitle.Text= _model.Title.Exp_Trim();
                txtSource.Text = _model.Source.Exp_Trim();
                txtContent.Value = _model.Contents.Exp_Trim();
                HGdropCate.Text = _model.CateID.Exp_Trim();
                txtSEOtitle.Text = _model.WebTitle.Exp_Trim();
                txtSEOkey.Text = _model.WebKey.Exp_Trim();
                txtSEOdec.Text = _model.WebSummary.Exp_Trim();
                //置顶为1，不置顶为0
                this.rdoListTopis.Text = _model.Topis.Exp_Trim();
                //推荐为1，不推荐为0
                this.rblRem.Text = _model.RemSign ? "1" : "0";
                txtColorKey.Text = _model.ColorKey.Exp_Trim();
                txtColorTitle.Text = _model.ColorTitle.Exp_Trim();
                txtInKey.Text = _model.InnerKey.Exp_Trim();
            });
        }

         /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void WebUserToolBar1_Button0(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
            {
                //操作日志类型
                Log_Type ltype = new Log_Type();

                _model.id = ViewState["id"].Exp_IntTryParse();
                _model.Title = this.txtTitle.Text.Exp_Trim();
                _model.Source = this.txtSource.Text.Exp_Trim();
                _model.Contents = this.txtContent.Value.Exp_Trim();
                _model.CateID = this.HGdropCate.SelectedValue.Exp_IntTryParse();
                _model.AddDate = DateTime.Now;
                //置顶
                _model.Topis = this.rdoListTopis.Text.Exp_IntTryParse();
                //推荐
                _model.RemSign = this.rblRem.Text == "0" ? false : true;
                _model.WebKey = this.txtSEOkey.Text != "" ? this.txtSEOkey.Text.Exp_Trim() : this.txtTitle.Text.Exp_Trim();
                _model.WebSummary = this.txtSEOdec.Text != "" ? this.txtSEOdec.Text.Exp_Trim() : this.txtTitle.Text.Exp_Trim();
                _model.WebTitle = this.txtSEOtitle.Text != "" ? this.txtSEOtitle.Text.Exp_Trim() : this.txtTitle.Text.Exp_Trim();
                _model.ColorKey = this.txtColorKey.Text != "" ? this.txtColorKey.Text.Exp_Trim() : this.txtTitle.Text.Exp_Trim();
                _model.ColorTitle = this.txtColorTitle.Text!=""? this.txtColorTitle.Text.Exp_Trim():this.txtTitle.Text.Exp_Trim();
                _model.InnerKey = this.txtInKey.Text != "" ? this.txtInKey.Text.Exp_Trim() : this.txtTitle.Text.Exp_Trim();
                //获取操作用户ID
                _model.UserID = UserConst.Exp_GetUser(this).id;
                var iRet = 0;
                if (_model.id > 0)
                {
                    var tempModel = _DAL.GetModel(_model.id);
                    if (tempModel == null || !tempModel.Flag)
                    {
                        this.Exp_PageAlter("当前信息不存在,可能已经被其他人删除!");
                        return;
                    }
                    iRet = _DAL.Update(_model);
                    ltype = Log_Type.add;
                }
                else
                {
                    iRet = _DAL.Add(_model);
                    ltype = Log_Type.update;
                }
                if (iRet > 0)
                {
                     // 写日志
                     this.Exp_PageWriteLog(ltype,"知识点信息", iRet);
                     var SKey = this.WebUserToolBar1.EditSKey;
                     this.Exp_PageAlterHref("信息操作成功！", "knowledgelist.aspx?" + SKey);
                }
                else
                {
                    this.Exp_PageAlter("操作失败");
                }
            });
        }

        /// <summary>
        /// 返回按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void WebUserToolBar1_Button1(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
            {
                var sKey = this.WebUserToolBar1.EditSKey;
                Response.Redirect("knowledgelist.aspx?" + sKey);
            });
        }
    }
}
